package com.vcredit.creditcat.time;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.common.threadpool.RequestLocalThread;
import com.vcredit.bird.service.creditTips.CreditTipsService;
import com.vcredit.creditcat.util.ConcurrentDateUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;

/**
 * 信用贴士内容采集后台任务
 */
@JobHander(value="creditTipTime")
@Component
public class CreditTipTime extends IJobHandler {
	private static Logger log = LoggerFactory.getLogger(CreditTipTime.class);

	@Autowired
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;

	@Autowired
	private CreditTipsService creditEntityService;

	@Override
	public ReturnT<String> execute(String... strings) throws Exception {
		log.info("信用贴士内容采集定时任务执行了................" + ConcurrentDateUtil.formatDateTime(new Date()));
		RequestLocalThread.create();
		try {
			threadPoolTaskExecutor.execute(new CrawleThread());
		} catch (Exception e) {
			log.error(e.getMessage());
			return ReturnT.FAIL;
		}
		return ReturnT.SUCCESS;
	}

	class CrawleThread implements Runnable {
		private Logger logger = LoggerFactory.getLogger(CrawleThread.class);

		public CrawleThread() {
			super();
		}

		public void run() {
			try {
				creditEntityService.creditEntityQuery();
			} catch (Exception e) {
				logger.error("Exception occured", e);
			}
		}
	}
}
